package controller

import (
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"kf/models/dao"
	"kf/models/entity"
	"kf/utils/R"
	"net/http"
)

type SysRuleController struct {
	SysRuleDao *dao.SysRuleDao
	Log        *zap.Logger
}

// 创建规则
func (sysRuleController *SysRuleController) SaveRule(c *gin.Context) {
	var sysRule entity.SysRule
	err := c.ShouldBindJSON(&sysRule)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	value, exists := c.Get("kefu_id")
	if !exists {
		sysRuleController.Log.Error("获取当前用户失败")
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	toInt, err := convertor.ToInt(value)
	if err != nil {
		sysRuleController.Log.Error("获取当前用户失败")
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	sysRule.CreateBy = toInt
	err = sysRuleController.SysRuleDao.SaveSysRule(sysRule)
	if err != nil {
		c.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	c.JSON(http.StatusOK, R.ReturnSuccessMsg("ok"))
}

// 删除
func (sysRuleController *SysRuleController) DeleteRule(context *gin.Context) {
	var sysRule entity.SysRule
	err := context.ShouldBindJSON(&sysRule)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	err = sysRuleController.SysRuleDao.DeleteSysRule(sysRule)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccessMsg("ok"))
}

// 修改客服规则
func (sysRuleController *SysRuleController) UpdateRule(context *gin.Context) {
	var sysRule entity.SysRuleForm
	err := context.ShouldBindJSON(&sysRule)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	err = sysRuleController.SysRuleDao.UpdateSysRule(sysRule)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccessMsg("ok"))
}

// 根据id查询
func (sysRuleController *SysRuleController) GetRule(context *gin.Context) {
	var sysRule entity.SysRule
	err := context.ShouldBindJSON(&sysRule)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	rule, err := sysRuleController.SysRuleDao.GetSysRule(sysRule)
	if err != nil {
		return
	}
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(rule))
}

// 分页获取规则
func (sysRuleController *SysRuleController) GetRulePage(context *gin.Context) {
	var page entity.RulePageQuery
	err := context.ShouldBindQuery(&page)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	rule, err := sysRuleController.SysRuleDao.GetSysRulePage(page)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	count, err := sysRuleController.SysRuleDao.GetSysRuleCount(page.Host, page.Ip)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"list":  rule,
			"count": count,
		},
	})
}
